// ---------------------------------------------------------------------
//
// -- 通用弹出窗口
// -- author £
//
// ---------------------------------------------------------------------

cc.Class({
    extends: cc.Component,

    properties: {
        FADE_TIME: {
            default: 0.2,
            tooltip: '显示动画时间'
        },
        SHOW_OFFSET: {
            default: 30,
            tooltip: '显示初始偏移'
        },

        btnClose: cc.Button,
        btnEnsure: cc.Button,
        content: cc.Label,
        gameMask: require('GameMask'),
        /** 是否是初始就已经添加在场景中的窗口 */
        isAdded: false,
        /** 是否点击蒙版关闭窗口 */
        clickMaskClose: false,
    },

    ctor: function () {
        this.initCtor();
    },

    initCtor: function () {
        this._closeFunc = null;
        this._closeData = null;
        this._closeScope = null;
        this._isShow = false;
    },

    // use this for initialization
    onLoad: function () {
        if (this.isAdded)
            this._initY = this.node.y;

        if (this.btnClose)
            this.btnClose.node.on('click', this.close, this);

        if (this.btnEnsure)
            this.btnEnsure.node.on('click', this.ensureHandler, this);

        if (this.isAdded) {
            this.node.active = false;
        }

        if (this.clickMaskClose && this.gameMask) {
            this.gameMask.node.on('touchend', this.close, this);
        }
    },

    start: function () {
    },

    close: function () {
        if (!this.isAdded)
            this.node.parent.emit('closeWindow');

        this.hide();
    },

    setData: function (content, hAlign, closeFunc, closeScope, closeData) {
        if (hAlign && content)
            this.content.horizontalAlign = hAlign;

        if (content)
            this.content.string = content;

        if (closeFunc) {
            if (!closeData)
                closeData = [];

            if (!closeScope)
                closeScope = this;

            this._closeFunc = closeFunc;
            this._closeScope = closeScope;
            this._closeData = closeData;
        }
    },

    show: function (parent) {
        if (this._isShow) return;

        this._isShow = true;

        if (this.gameMask)
            this.gameMask.show();

        if (this.isAdded) {
            this.node.active = true;
        }
        else {
            this.node.parent = parent;
            this._initY = this.node.y;
        }

        require('ActionLib').FADE_MOVE_SHOW(
            this.node, this.FADE_TIME,
            null, this._initY + this.SHOW_OFFSET,
            null, this._initY
        );
        // this.node.resumeSystemEvents(true);
        // console.log('resumeSystemEvents');
    },

    hide: function (directly) {
        if (!this._isShow) return;

        this._isShow = false;

        if (this.gameMask)
            this.gameMask.hide();

        // this.node.pauseSystemEvents(true);
        // console.log('pauseSystemEvents');

        if (directly) {
            this.hideEnd();
            return;
        }
        
        require('ActionLib').FADE_MOVE_HIDE(
            this.node, this.FADE_TIME,
            null, this._initY + this.SHOW_OFFSET, null,
            this.hideEnd, this
        );
    },

    hideEnd: function () {
        if (this.isAdded)
            this.node.active = false;
        else
            this.node.parent = null;
    },

    ensureHandler: function () {
        this.close();

        if (this._closeFunc) {
            this._closeFunc.apply(this._closeScope, this._closeData);
        }
    },

    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
